/**
 * 
 */
package com.carnation.qa.socketserver.proxy.moi;

import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.carnation.mong.platform.domain.MOIDomain;
import com.carnation.mong.platform.service.DomainService;
import com.carnation.qa.socketserver.domain.Item;
import com.carnation.qa.socketserver.proxy.Builder;

/**
 * @author Nick
 * 
 */
public class DomainServiceImpl extends AbstractMOIServiceImpl {

	private final Log log = LogFactory.getLog(getClass());

	private DomainService service;

	public DomainServiceImpl(List<Item> items) {
		super(items);
		service = ServiceFactory.getService(DomainService.class);
	}

	@Override
	protected String invoke() {
		Item item = getItems().get(0);
		String method = getMethodName(item);
		String returnStr;
		if ("create".equals(method)) {
			returnStr = create(item);
		} else if ("delete".equals(method)) {
			returnStr = delete(item);
		} else if ("getAllDomainNodes".equals(method)) {
			returnStr = getAllDomainNodes(item);
		} else if ("getAllDomains".equals(method)) {
			returnStr = getAllDomains(item);
		} else if ("getDefaultDomain".equals(method)) {
			returnStr = getDefaultDomain(item);
		} else if ("getDomain".equals(method)) {
			returnStr = getDomain(item);
		} else if ("getDomainByName".equals(method)) {
			returnStr = getDomainByName(item);
		} else if ("update".equals(method)) {
			returnStr = update(item);
		} else {
			throw new IllegalArgumentException(method);
		}
		return returnStr;
	}

	private String create(Item item) {
		if (item.getChildren().size() == 1) {// 个参数
			MOIDomain domain = instanceMOIDomain(item.getChildren().get(0));
			if (log.isDebugEnabled()) {
				log.debug("MOIDomain:" + domain);
			}
			Object obj = null;
			try {
				obj = service.create(domain);
				if (log.isDebugEnabled()) {
					log.debug(obj);
				}
				return new Builder(obj, null, item.getTabSeparator())
						.toString();
			} catch (Exception e) {
				if (log.isErrorEnabled()) {
					log.error(item.toString(), e);
				}
				return e.toString();
			}
		}
		return IllegalArgumentException.class.getSimpleName();
	}

	private String delete(Item item) {
		if (item.getChildren().size() == 1) {// 个参数
			MOIDomain domain = instanceMOIDomain(item.getChildren().get(0));
			if (log.isDebugEnabled()) {
				log.debug("MOIDomain:" + domain);
			}
			Object obj = null;
			try {
				service.delete(domain);
				obj = true;
				if (log.isDebugEnabled()) {
					log.debug(obj);
				}
				return new Builder(obj, null, item.getTabSeparator())
						.toString();
			} catch (Exception e) {
				if (log.isErrorEnabled()) {
					log.error(item.toString(), e);
				}
				return e.toString();
			}
		}
		return IllegalArgumentException.class.getSimpleName();
	}

	private String getAllDomainNodes(Item item) {
		Object obj = null;
		try {
			obj = service.getAllDomainNodes();
			if (log.isDebugEnabled()) {
				log.debug(obj);
			}
			return new Builder(obj, null, item.getTabSeparator()).toString();
		} catch (Exception e) {
			if (log.isErrorEnabled()) {
				log.error(item.toString(), e);
			}
			return e.toString();
		}
	}

	private String getAllDomains(Item item) {
		Object obj = null;
		try {
			obj = service.getAllDomains();
			if (log.isDebugEnabled()) {
				log.debug(obj);
			}
			return new Builder(obj, null, item.getTabSeparator()).toString();
		} catch (Exception e) {
			if (log.isErrorEnabled()) {
				log.error(item.toString(), e);
			}
			return e.toString();
		}
	}

	private String getDefaultDomain(Item item) {
		Object obj = null;
		try {
			obj = service.getDefaultDomain();
			if (log.isDebugEnabled()) {
				log.debug(obj);
			}
			return new Builder(obj, null, item.getTabSeparator()).toString();
		} catch (Exception e) {
			if (log.isErrorEnabled()) {
				log.error(item.toString(), e);
			}
			return e.toString();
		}
	}

	private String getDomain(Item item) {
		if (item.getChildren().size() == 1) {// 个参数
			String id = getString(item.getChildren().get(0).getLabelParts()[1]);
			if (log.isDebugEnabled()) {
				log.debug("id:" + id);
			}
			Object obj = null;
			try {
				obj = service.getDomain(id);
				if (log.isDebugEnabled()) {
					log.debug(obj);
				}
				return new Builder(obj, null, item.getTabSeparator())
						.toString();
			} catch (Exception e) {
				if (log.isErrorEnabled()) {
					log.error(item.toString(), e);
				}
				return e.toString();
			}
		}
		return IllegalArgumentException.class.getSimpleName();
	}

	private String getDomainByName(Item item) {
		if (item.getChildren().size() == 1) {// 个参数
			String name = getString(item.getChildren().get(0).getLabelParts()[1]);
			if (log.isDebugEnabled()) {
				log.debug("name:" + name);
			}
			Object obj = null;
			try {
				obj = service.getDomainByName(name);
				if (log.isDebugEnabled()) {
					log.debug(obj);
				}
				return new Builder(obj, null, item.getTabSeparator())
						.toString();
			} catch (Exception e) {
				if (log.isErrorEnabled()) {
					log.error(item.toString(), e);
				}
				return e.toString();
			}
		}
		return IllegalArgumentException.class.getSimpleName();
	}

	private String update(Item item) {
		if (item.getChildren().size() == 1) {// 个参数
			MOIDomain domain = instanceMOIDomain(item.getChildren().get(0));
			if (log.isDebugEnabled()) {
				log.debug("MOIDomain:" + domain);
			}
			Object obj = null;
			try {
				service.update(domain);
				obj = true;
				if (log.isDebugEnabled()) {
					log.debug(obj);
				}
				return new Builder(obj, null, item.getTabSeparator())
						.toString();
			} catch (Exception e) {
				if (log.isErrorEnabled()) {
					log.error(item.toString(), e);
				}
				return e.toString();
			}
		}
		return IllegalArgumentException.class.getSimpleName();
	}

}
